package com.housesales.web;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import module.WorkSchedule;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import utils.DateUtil;
import bussinessService.basicbussinessService;
import bussinessService.impl.WorkScheduleBusinessServiceImpl;
import domain.vo.JSONObject;
import domain.vo.Message;
import domain.vo.WorkScheduleQueryObject;
import domain.vo.WorkScheduleVO;

/**
 * 
* @ClassName: WorkScheduleController 
* @Description: WorkScheduleController
* @author chenchao
* @date 2014年2月17日 上午10:33:07 
*
 */
@Controller
@RequestMapping("/WorkSchedule")
public class WorkScheduleController {
	private Logger logger = Logger.getLogger(WorkScheduleController.class.getName());
	
	/**
	 *	workSchedule业务层实现类
	 */
	private basicbussinessService<WorkSchedule,WorkScheduleVO>  workScheduleBSI = new WorkScheduleBusinessServiceImpl();
	
	
	@RequestMapping("{pathName}") 
	public ModelAndView index(@PathVariable("pathName") String pathName) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/admin/workschedule/"+ pathName);
		return mv;
	}
	
	
	
	@RequestMapping("/addWS")
	public @ResponseBody  Message saveWorkSchedule(HttpServletRequest request) {
		Message ms = new Message();
		try {
			String e_name = request.getSession().getAttribute("e_name").toString();
			
			WorkScheduleQueryObject queryObject = new WorkScheduleQueryObject();
			queryObject.setE_name(e_name);
			int addedNumber = workScheduleBSI.countByQueryObject(queryObject);
			if (addedNumber >=2) {
				logger.error(e_name + "重复打开");
				ms.setErrorMsg("今天已经打过卡了");
			} else {
				int shopId = Integer.valueOf(request.getSession().getAttribute("shopId").toString());
				WorkSchedule ws = new WorkSchedule();
				ws.setE_name(e_name);
				ws.setShopId(shopId);
				workScheduleBSI.merageObject(ws);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			ms.setErrorMsg("数据保存失败");
		}
		return ms;
	}
	
	@RequestMapping("/getWSJSON")
	public @ResponseBody JSONObject<WorkScheduleVO> getKPIJSON(HttpServletRequest req){
		String startWeekDay = (req.getParameter("startWeek") == null)?"":req.getParameter("startWeek").toString();
		String shopId = (req.getParameter("selshopId2") == null)?"":req.getParameter("selshopId2").toString();
		
		JSONObject<WorkScheduleVO> json = new JSONObject<WorkScheduleVO>();
		WorkScheduleQueryObject queryObject = new WorkScheduleQueryObject();
		List<WorkScheduleVO> vos = new LinkedList<WorkScheduleVO>();
		Calendar calendar = Calendar.getInstance();
		if (!"".equals(startWeekDay)) {
			try {
				calendar.setTime(DateFormat.getDateInstance().parse(startWeekDay));
			} catch (ParseException e) {
				e.printStackTrace();
				logger.error(e.getMessage());
			}
		}
		queryObject.setCalendar(calendar);
		queryObject.setShopId(((shopId.equals("") || shopId.equals("-1"))?"2":shopId));
		queryObject.setWeek(calendar.get(Calendar.WEEK_OF_YEAR));
		try {
			vos = workScheduleBSI.getObjectByQueryObject(queryObject);
			json.setRows(vos);
			json.setTotal(vos.size());
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return json;
	}
	
}
